<?php

declare(strict_types=1);

namespace think\auth\service;

use think\App;
use think\db\exception\DbException;
use think\auth\model\PermissionModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class Permission
{
    public function __construct(App $app)
    {
        $this->app = $app;
    }

    /**
     * 创建一个权限
     * @param string $permissionName
     * @return PermissionModel
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function Create(string $permissionName): PermissionModel
    {
        $permission = PermissionModel::FindByName($permissionName);
        if(!empty($permission) && !$permission->isEmpty()) return $permission;
        return PermissionModel::create(['name'=>$permissionName]);
    }

    /**
     * 获取一个权限
     * @param int|string $permissionId
     * @return PermissionModel
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function Fetch(int|string $permissionId): PermissionModel
    {
        return PermissionModel::FindById($permissionId);
    }
}
